﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace S2_Xxxx_XxxNetApi
{
    public static class DecimalCommon
    {
        /// <summary>
        /// 除去0的值
        /// </summary>
        /// <param name="input">输入值</param>
        /// <returns></returns>
        public static string ToStringNoZero(this Decimal input)
        {
            if(input<=0)
            {
                return "";
            }

            return input.ToString();
        }

        /// <summary>
        /// 除去0的值
        /// </summary>
        /// <param name="input">输入值</param>
        /// <returns></returns>
        public static string ToStringNoZero(this Decimal? input)
        {
            if (input != null)
            {
                if (input <= 0)
                {
                    return "";
                }

                return input.ToString();
            }

            return null;
        }

        /// <summary>
        /// 除去0的值
        /// </summary>
        /// <param name="input">输入值</param>
        /// <param name="defaultValue">缺省值</param>
        /// <returns></returns>
        public static Decimal ToDecimal(this Decimal? input, Decimal defaultValue)
        {
            if (input != null)
            {
                return Convert.ToDecimal(input);
            }

            return Convert.ToDecimal(0.00);
        }

        /// <summary>
        /// 除去0的值
        /// </summary>
        /// <param name="input">输入值</param>
        /// <returns></returns>
        public static Decimal ToDecimal(this Decimal? input)
        {
            return ToDecimal(input, Convert.ToDecimal(0.00));
        }

        /// <summary>
        /// 隔3位加逗号
        /// </summary>
        /// <param name="input">输入值</param>
        /// <returns></returns>
        public static String ToDecString(this Decimal input)
        {
            return input.ToString("C").Replace("￥", "");
        }

        /// <summary>
        /// 隔3位加逗号
        /// </summary>
        /// <param name="input">输入值</param>
        /// <returns></returns>
        public static string ToDecString(this object input)
        {
            if(input == null || String.IsNullOrEmpty(input.ToString()))
            {
                return "0.00";
            }
            else
            {
                try
                {
                    return Convert.ToDecimal(input).ToDecString();
                }
                catch(Exception)
                {
                    return "0.00";
                }
            }
        }
    }
}
